package com.ruoyi.lipin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lipin.domain.bo.LpCollectionBo;
import com.ruoyi.lipin.domain.vo.LpCollectionVo;
import com.ruoyi.lipin.domain.LpCollection;
import com.ruoyi.lipin.mapper.LpCollectionMapper;
import com.ruoyi.lipin.service.ILpCollectionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@RequiredArgsConstructor
@Service
public class LpCollectionServiceImpl implements ILpCollectionService {

    private final LpCollectionMapper baseMapper;

    /**
     * 查询用户收藏
     */
    @Override
    public LpCollectionVo queryById(Long collectionId){
        return baseMapper.selectVoById(collectionId);
    }

    /**
     * 查询用户收藏列表
     */
    @Override
    public TableDataInfo<LpCollectionVo> queryPageList(LpCollectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LpCollection> lqw = buildQueryWrapper(bo);
        Page<LpCollectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户收藏列表
     */
    @Override
    public List<LpCollectionVo> queryList(LpCollectionBo bo) {
        LambdaQueryWrapper<LpCollection> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LpCollection> buildQueryWrapper(LpCollectionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LpCollection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, LpCollection::getUserId, bo.getUserId());
        lqw.eq(bo.getProductId() != null, LpCollection::getProductId, bo.getProductId());
        return lqw;
    }

    /**
     * 新增用户收藏
     */
    @Override
    public Boolean insertByBo(LpCollectionBo bo) {
        LpCollection add = BeanUtil.toBean(bo, LpCollection.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCollectionId(add.getCollectionId());
        }
        return flag;
    }

    /**
     * 修改用户收藏
     */
    @Override
    public Boolean updateByBo(LpCollectionBo bo) {
        LpCollection update = BeanUtil.toBean(bo, LpCollection.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LpCollection entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户收藏
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
